home *** CD-ROM | disk | FTP | other *** search
/ The Atari Compendium / The Atari Compendium (Toad Computers) (1994).iso / files / prgtools / editors / pur_c_vi.zoo / cmdline.c next >
Encoding:
C/C++ Source or Header  |  1992-08-11  |  4.7 KB  |  254 lines

  1. /*
  2.  * STevie - ST editor for VI enthusiasts.   ...Tim Thompson...twitch!tjt...
  3.  */
  4.  
  5. #include <stdio.h>
  6. #include <ctype.h>
  7. #include "stevie.h"
  8.  
  9. readcmdline(firstc)
  10. int firstc;    /* either ':', '/', or '?' */
  11. {
  12.     int c;
  13.     char buff[100];
  14.     char *p, *q, *cmd, *arg;
  15.  
  16.     gotocmd(1,1,firstc);
  17.     p = buff;
  18.     if ( firstc != ':' )
  19.         *p++ = firstc;
  20.     /* collect the command string, handling '\b' and @ */
  21.     for ( ; ; ) {
  22.         c = vgetc();
  23.         if ( c=='\n'||c=='\r'||c==EOF )
  24.             break;
  25.         if ( c=='\b' ) {
  26.             if ( p > buff ) {
  27.                 p--;
  28.                 /* I know this is gross, but it has the */
  29.                 /* advantage of relying only on 'gotocmd' */
  30.                 gotocmd(1,0,firstc==':'?':':0);
  31.                 for ( q=buff; q<p; q++ )
  32.                     windputc(*q);
  33.                 windrefresh();
  34.             }
  35.             continue;
  36.         }
  37.         if ( c=='@' ) {
  38.             p = buff;
  39.             gotocmd(1,1,firstc);
  40.             continue;
  41.         }
  42.         windputc(c);
  43.         windrefresh();
  44.         *p++ = c;
  45.     }
  46.     *p = '\0';
  47.  
  48.     /* skip any initial white space */
  49.     for ( cmd = buff; isspace(*cmd); cmd++ )
  50.         ;
  51.  
  52.     /* search commands */
  53.     c = *cmd;
  54.     if ( c == '/' || c == '?' ) {
  55.         cmd++;
  56.         if ( *cmd == c ) {
  57.             /* the command was '//' or '??' */
  58.             repsearch();
  59.             return;
  60.         }
  61.         /* If there is a matching '/' or '?' at the end, toss it */
  62.         p = strchr(cmd,'\0');
  63.         if ( *(--p) == c )
  64.             *p = '\0';
  65.         dosearch(c=='/'?FORWARD:BACKWARD,cmd);
  66.         return;
  67.     }
  68.  
  69.     /* isolate the command and find any argument */
  70.     for ( p=cmd; *p!='\0' && ! isspace(*p); p++ )
  71.         ;
  72.     if ( *p == '\0' )
  73.         arg = NULL;
  74.     else {
  75.         *p = '\0';
  76.         while ( *(++p) != '\0' && isspace(*p) )
  77.             ;
  78.         arg = p;
  79.         if ( *arg == '\0' )
  80.             arg = NULL;
  81.     }
  82.     if ( strcmp(cmd,"q!")==0 )
  83.         getout();
  84.     if ( strcmp(cmd,"q")==0 ) {
  85.         if ( Changed )
  86.             message("File not written out.  Use 'q!' to override.");
  87.         else
  88.             getout();
  89.         return;
  90.     }
  91.     if ( strcmp(cmd,"w")==0 ) {
  92.         if ( arg == NULL ) {
  93.             writeit(Filename);
  94.             UNCHANGED;
  95.         }
  96.         else
  97.             writeit(arg);
  98.         return;
  99.     }
  100.     if ( strcmp(cmd,"wq")==0 ) {
  101.         if ( writeit(Filename) )
  102.             getout();
  103.         return;
  104.     }
  105.     if ( strcmp(cmd,"f")==0 && arg==NULL ) {
  106.         fileinfo();
  107.         return;
  108.     }
  109.     if ( strcmp(cmd,"e") == 0 || strcmp(cmd,"e!") == 0 ) {
  110.         if ( cmd[1]!='!' && Changed ) {
  111.             message("File not written out.  Use 'e!' to override.");
  112.         }
  113.         else {
  114.             if ( arg != NULL )
  115.                 Filename = strsave(arg);
  116.             /* clear mem and read file */
  117.             Fileend = Topchar = Curschar = Filemem;
  118.             UNCHANGED;
  119.             p = nextline(Curschar);
  120.             readfile(Filename,Fileend,0);
  121.             updatescreen();
  122.         }
  123.         return;
  124.     }
  125.     if ( strcmp(cmd,"f") == 0 ) {
  126.         Filename = strsave(arg);
  127.         filemess("");
  128.         return;
  129.     }
  130.     if ( strcmp(cmd,"r") == 0 || strcmp(cmd,".r") == 0 ) {
  131.         char *pp;
  132.         if ( arg == NULL ) {
  133.             badcmd();
  134.             return;
  135.         }
  136.         /* find the beginning of the next line and */
  137.         /* read file in there */
  138.         pp = nextline(Curschar);
  139.         readfile(arg,pp,1);
  140.         updatescreen();
  141.         CHANGED;
  142.         return;
  143.     }
  144.     if ( strcmp(cmd,".=")==0 ) {
  145.         char messbuff[80];
  146.         sprintf(messbuff,"line %d   character %d",
  147.             cntlines(Filemem,Curschar),
  148.             1+(int)(Curschar-Filemem));
  149.         message(messbuff);
  150.         return;
  151.     }
  152.     if ( strcmp(cmd,"$=")==0 ) {
  153.         char messbuff[8];
  154.         sprintf(messbuff,"%d",
  155.             cntlines(Filemem,Fileend)-1);
  156.         message(messbuff);
  157.         return;
  158.     }
  159.     if ( strcmp(cmd,"set")==0 ) {
  160.         if ( arg == NULL )
  161.             badcmd();
  162.         else if ( strcmp(arg,"oct")==0 ) {
  163.             octchars();
  164.             updatescreen();
  165.         }
  166.         else if ( strcmp(arg,"hex")==0 ) {
  167.             hexchars();
  168.             updatescreen();
  169.         }
  170.         else if ( strcmp(arg,"dec")==0 ) {
  171.             decchars();
  172.             updatescreen();
  173.         }
  174.         else
  175.             badcmd();
  176.         return;
  177.     }
  178.     badcmd();
  179. }
  180.  
  181. badcmd()
  182. {
  183.     message("Unrecognized command");
  184. }
  185.  
  186. gotocmd(clr,fresh,firstc)
  187. {
  188.     int n;
  189.  
  190.     windgoto(Rows-1,0);
  191.     if ( clr ) {
  192.         /* clear the line */
  193.         for ( n=0; n<(Columns-1); n++ )
  194.             windputc(' ');
  195.         windgoto(Rows-1,0);
  196.     }
  197.     if ( firstc )
  198.         windputc(firstc);
  199.     if ( fresh )
  200.         windrefresh();
  201. }
  202.  
  203. message(s)
  204. char *s;
  205. {
  206.     static char *lastmess = NULL;
  207.     char *p;
  208.  
  209.     if ( lastmess!=NULL ) {
  210.         if ( strcmp(lastmess,s)==0 )
  211.             return;
  212.         free(lastmess);
  213.     }
  214.     gotocmd(1,1,0);
  215.     /* take off any trailing newline */
  216.     if ( (p=strchr(s,'\0'))!=NULL && *p=='\n' )
  217.         *p = '\0';
  218.     windstr(s);
  219.     lastmess = strsave(s);
  220. }
  221.  
  222. writeit(fname)
  223. char *fname;
  224. {
  225.     FILE *f;
  226.     char buff[128];
  227.     char *p;
  228.     int n;
  229.  
  230. #ifdef ATARI
  231.     if ( (f=fopen(fname,Binary?"bw":"w")) == NULL ) {
  232. #else
  233.     if ( (f=fopen(fname,"w")) == NULL ) {
  234. #endif
  235.         message("Unable to open file!");
  236.         return(0);
  237.     }
  238.     for ( n=0,p=Filemem; p<Fileend; p++,n++ )
  239.         putc(*p,f);
  240.     fclose(f);
  241.     sprintf(buff,"\"%s\" %d characters",fname,n);
  242.     message(buff);
  243.     UNCHANGED;
  244.     return(1);
  245. }
  246.  
  247. filemess(s)
  248. char *s;
  249. {
  250.     char buff[128];
  251.     sprintf(buff,"\"%s\" %s",Filename,s);
  252.     message(buff);
  253. }
  254.